class code1 {
    //暴力+hash
    public int lengthOfLongestSubstring(String s) {
        int ret = 0;
        int sta = 0;
        //保存字符下标，下一次从这个位置开始
        HashMap hash = new HashMap<Character,Integer>();

        for(int i=0;i<s.length();i++) {
            char ch = s.charAt(i);
            if(hash.containsKey(ch)) {
                int tsta = (int)(hash.get(ch))+1;
                //原来hash元素的下标还在前面，那么就更新
                if(tsta > sta) {
                    sta = tsta;
                }
            }
            ret = Math.max(ret,i-sta+1);
            hash.put(ch,i);
        }
        return ret;
    }
}